package com.dotstone.chipism.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.EventBusEntity;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.bean.Scene;
import com.dotstone.chipism.ui.timing.AddSocketTimingActivity;
import com.dotstone.chipism.ui.timing.RemoteSocketTimingActivity;
import com.dotstone.chipism.ui.timing.bean.AddTimingBean;
import com.dotstone.chipism.ui.timing.bean.SocketTimingData;
import com.dotstone.chipism.ui.view.DevicePopWindow;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.view.SancePopWindow;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class WirelessSwitchActivity extends BaseActivity {

    public static TextView tvTitle;
    private View btnBack;
    public static Button btn_switch3;
    public static Button btn_switch2;
    public static Button btn_switch1;
    public  Button btnAdd;
    private TextView mStatusBar;
    private int devicetype;
    private String deviceid;
    private int position;
    private List<Scene> list;

    long firstPressTime = 0;
    long mNow = 0;
    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    break;
                case 2:
                  //  Snackbar.make(getCurrentFocus(),"单击事件",Snackbar.LENGTH_SHORT).show();
                    break;
                case 3:

                    break;
                case 4:
                    //  Snackbar.make(getCurrentFocus(),"单击事件",Snackbar.LENGTH_SHORT).show();
                    break;
                case 5:

                    break;
                case 6:
                    //  Snackbar.make(getCurrentFocus(),"单击事件",Snackbar.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    private boolean isdelete=false;


    @Override
    public void widgetClick(View v) {
        int position = 0;
        int bsid=-1;
         String sceneid="";
        switch (v.getId()) {
            case R.id.btn_switch1:
                if(devicetype==73){
                    position=50;
                    if(list.size()>0){
                    for (int j=0;j<list.size();j++){
                        if(list.get(j).getPosition()==50){
                            bsid=list.get(j).getBsId();
                            sceneid=list.get(j).getSceneId();
                        }
                      }
                    }
                }else if(devicetype==74){
                    position=51;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==51){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }else if(devicetype==75){
                    position=51;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==51){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }
                new SancePopWindow(WirelessSwitchActivity.this,v,position,deviceid,bsid,sceneid)
                        .showPopupWindow(btn_switch1);
                break;
            case R.id.btn_switch2:
                if(devicetype==74){
                    position=49;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==49){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }else{
                    position=50;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==50){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }
                new SancePopWindow(WirelessSwitchActivity.this,v,position,deviceid,bsid,sceneid)
                        .showPopupWindow(btn_switch2);
                break;
            case R.id.btn_switch3:
                if(devicetype==75){
                    position=49;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==49){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }else{
                    position=51;
                    if(list.size()>0){
                        for (int j=0;j<list.size();j++){
                            if(list.get(j).getPosition()==51){
                                bsid=list.get(j).getBsId();
                                sceneid=list.get(j).getSceneId();
                            }
                        }
                    }
                }
                new SancePopWindow(WirelessSwitchActivity.this,v,position,deviceid,bsid,sceneid)
                        .showPopupWindow(btn_switch3);
                break;
            case R.id.nextstep:

                break;
            case R.id.back_layout:
                finish();
                break;
        }
    }


    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
         return R.layout.activity_wireless_switch;
    }

    @Override
    public void initView(View view) {
        EventBus.getDefault().register(this);
        btnBack = $(R.id.back_layout);
        tvTitle = $(R.id.title_tv);
        btnAdd = $(R.id.nextstep);
        mStatusBar = $(R.id.tv_statusBar_mian);
        btn_switch1 = $(R.id.btn_switch1);
        btn_switch2 = $(R.id.btn_switch2);
        btn_switch3 = $(R.id.btn_switch3);
        list=new ArrayList<>();
        devicetype = getIntent().getIntExtra("devicetype",75);
        position = getIntent().getIntExtra("position",50);
        deviceid=getIntent().getStringExtra("deviceid");
        if(devicetype==73){
            btn_switch1.setVisibility(View.VISIBLE);
            btn_switch2.setVisibility(View.GONE);
            btn_switch3.setVisibility(View.GONE);
        }else  if(devicetype==74){
            btn_switch1.setVisibility(View.VISIBLE);
            btn_switch2.setVisibility(View.VISIBLE);
            btn_switch3.setVisibility(View.GONE);
        }  if(devicetype==75){
            btn_switch1.setVisibility(View.VISIBLE);
            btn_switch2.setVisibility(View.VISIBLE);
            btn_switch3.setVisibility(View.VISIBLE);
        }
        boolVersion();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void getEventBusMessage(EventBusEntity entity) {
        switch (entity.getOption()) {
            case "success":
                isdelete=false;
                queryData();
                break;
            case "delete_success":
                list.clear();
                isdelete=true;
                if(devicetype==73){
                    if( entity.getValue()==50){
                        btn_switch1.setText("开关1");
                    }
                }else if(devicetype==74){
                    if( entity.getValue()==51){
                        btn_switch1.setText("开关1");
                    }else if( entity.getValue()==49){
                        btn_switch2.setText("开关2");
                    }
                }else{
                    if( entity.getValue()==51){
                        btn_switch1.setText("开关1");
                    }else if( entity.getValue()==50){
                        btn_switch2.setText("开关2");
                    }else if( entity.getValue()==49){
                        btn_switch3.setText("开关3");
                    }
                }

                queryData();
                break;
            default:
                break;
        }
    }



    @Override
    public void setListener() {
        tvTitle.setText("无线开关");
        btnAdd.setText("确定");
        btnAdd.setVisibility(View.GONE);
        btnBack.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        btn_switch3.setOnClickListener(this);
        btn_switch2.setOnClickListener(this);
        btn_switch1.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        queryData();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("onActivityResult" ,"进来了" + requestCode + "---" + resultCode);
        if ( resultCode == SceneSettingActivity.ADD_SUCCESS_RESULT_CODE){
            queryData();
        }
    }

    /**
     * 查询数据
     */
    private void queryData(){
       // mSVProgressHUD.showWithStatus("正在获取数据");
        JSONObject obj = new JSONObject();
        try{
          //  obj.put("hid",  DeviceManager.getInstance().hid);
            obj.put("key", "10000");
            obj.put("deviceId", deviceid);
            obj.put("requestTime", "" + System.currentTimeMillis());
            obj.put("token", (String) SPUtils.get(getApplicationContext(), "token", ""));
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        //查看情景数据
        OkHttpUtils.postString().url(HttpURL.SCENE_QUEERY)
                .mediaType(MediaType.parse("application/json;charset=UTF-8"))
                .content(obj.toString())
                .build().readTimeOut(5000)
                .writeTimeOut(5000).connTimeOut(5000)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        showToast("获取数据失败");
                       // mSVProgressHUD.dismiss();
                    }

                    @Override
                    public void onResponse(String response) {
                      //  mSVProgressHUD.dismiss();
                        Log.i("我我我", "response");
                            JSONObject jsonObject = null;
                            String value;
                            JSONArray jArray;
                            try {
                                jsonObject = new JSONObject(response);
                                 value = jsonObject.getString("resultCode");
                                jArray = (JSONArray) jsonObject.opt("data");
                                if (value.equals("1")){
                                    for (int i = 0; i < jArray.length(); i++) {
                                        JSONObject o = (JSONObject) jArray.get(i);
                                        int sceneId = (int) o.get("sceneId");
                                        int position = (int) o.get("position");
                                        int bsId = (int) o.get("bsId");
                                        Scene scene=new Scene();
                                        scene.setSceneId(""+sceneId);
                                        scene.setPosition(position);
                                        scene.setBsId(bsId);
                                        list.add(scene);
                                    }
                                    if(!isdelete){
                                        for (int j=0;j<list.size();j++){
                                            if(devicetype==73){
                                                if(list.get(j).getPosition()==50){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null){
                                                        btn_switch1.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }
                                                }
                                            }else if(devicetype==74){
                                                 if(list.get(j).getPosition()==51){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null){
                                                        btn_switch1.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }
                                                }
                                                if(list.get(j).getPosition()==49){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null) {
                                                        btn_switch2.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }

                                                }
                                            }else if(devicetype==75){
                                                if(list.get(j).getPosition()==51){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null){
                                                        btn_switch1.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }
                                                }
                                                if(list.get(j).getPosition()==50){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null){
                                                        btn_switch2.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }
                                                }
                                                if(list.get(j).getPosition()==49){
                                                    if(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()) != null){
                                                        btn_switch3.setText(DeviceManager.getInstance().getSceneById(list.get(j).getSceneId()).getSceneName());
                                                    }
                                                }
                                            }

                                        }
                                    }

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                    }
                });
    }

    public void boolVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            // getWindow().addFlags(
            // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            // 改变titlebar的高度
            int statusbarHeight = BarUtils.getStatusBarHeight(this);
            RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
            layoutParams1.height = statusbarHeight;
            mStatusBar.setLayoutParams(layoutParams1);
            mStatusBar.setVisibility(View.VISIBLE);
        } else {
            mStatusBar.setVisibility(View.GONE);
        }
    }



}
